package com.atguigu.zk.server;

import com.atguigu.constant.CommonConstantValue;
import com.atguigu.zk.util.ZkUtil;
import org.apache.curator.framework.CuratorFramework;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;

/**
 * @ClassName Route
 * @DeScription Route
 * @Author 剧情再美终是戏
 * @Date 2019/11/22 21:00
 * @Version 1.0
 **/
public class Route {

    private static CuratorFramework curatorFramework;

    // 获取数据并监听
    private static void getDatas(final String path) throws Exception {

        byte[] data = ZkUtil.getData(curatorFramework, path, new Watcher() {
            public void process(WatchedEvent watchedEvent) {
                try {
                    getDatas(path);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
        System.out.println("setData 结果为：" + new String(data));
    }

    public static void main(String[] args) {
        // 初始化
        init();

        // 获取数据并监听
//        getDatas("/server");

        // 其他操作
        otherOperation();
    }

    private static void otherOperation() {
       while (true) {
           System.out.println("我正在在做其他事情");
       }
    }

    private static void init() {
        curatorFramework = ZkUtil.getConnection(CommonConstantValue.ZK_CONNECTION_ADRESS, "server");
//        curatorFramework.getChildren().w
    }
}
